package com.wtp.wallpaper.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wtp.wallpaper.common.constant.MessageConstant;
import com.wtp.wallpaper.common.exception.TabExistException;
import com.wtp.wallpaper.pojo.dto.TabDTO;
import com.wtp.wallpaper.pojo.entity.Tabs;
import com.wtp.wallpaper.mapper.TabsMapper;
import com.wtp.wallpaper.service.TabsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class TabsServiceImpl extends ServiceImpl<TabsMapper, Tabs> implements TabsService {


    @Override
    public String removeTabsByIds(TabDTO tabDTO) {

        String[] removeIds = tabDTO.getRemoveIds();
        ArrayList<String> ids = new ArrayList<>(List.of(removeIds));
        //todo 删除的时候看标签是否被引用
        removeByIds(ids);
        return "";
    }

    @Override
    public void addOne(TabDTO tabDTO) {
        Tabs tabs = BeanUtil.copyProperties(tabDTO, Tabs.class);
        tabs.setCreateTime(LocalDateTime.now());
        tabs.setUpdateTime(LocalDateTime.now());

        if(exists(new QueryWrapper<Tabs>().eq("tab_name",tabs.getTabName()))){
            throw new TabExistException(MessageConstant.TABS_EXISTS);
        }

        log.info("数据转换:{}",tabs);
        save(tabs);
    }

    @Override
    public void update(TabDTO tabDTO) {
        Tabs tabs = BeanUtil.copyProperties(tabDTO, Tabs.class);
        try{
            tabs.setUpdateTime(LocalDateTime.now());
            updateById(tabs);
        }catch (Exception e){
            throw new TabExistException(MessageConstant.TABS_EXISTS);
        }

    }
}
